package org.budo.launcher.netty.servlet;

import java.util.EnumSet;

import javax.servlet.DispatcherType;
import javax.servlet.ServletRegistration.Dynamic;

import org.budo.launcher.service.config.AbstractWebXmlConfigParser;
import org.budo.launcher.service.config.FilterConf;
import org.budo.launcher.service.config.FilterMappingConf;
import org.budo.launcher.service.config.ServletConf;
import org.budo.netty.servlet.server.BudoServletServer;

import lombok.extern.slf4j.Slf4j;

/**
 * @author lmw
 */
@Slf4j
public class BudoNettyServletWebXmlConfigParser extends AbstractWebXmlConfigParser {
    private BudoServletServer budoServletServer;

    public BudoNettyServletWebXmlConfigParser(BudoServletServer budoServletServer) {
        this.budoServletServer = budoServletServer;
    }

    @Override
    public void addContextParam(String paramName, String paramValue) {
        log.warn("#36 addContextParam, paramName=" + paramName + ", paramValue=" + paramValue);

        budoServletServer.getServletContext() //
                .setInitParameter(paramName, paramValue);
    }

    @Override
    public void addListener(String listenerClass) {
        log.warn("#36 addListener, listenerClass=" + listenerClass);

        budoServletServer.getServletContext() //
                .addListener(listenerClass);
    }

    @Override
    public void addFilter(FilterConf filterConf) {
        log.warn("#42 addFilter, filterConf=" + filterConf);

        budoServletServer.getServletContext() //
                .addFilter(filterConf.getFilterName(), filterConf.getFilterClass()) //
                .setInitParameters(filterConf.getInitParamMap());
    }

    @Override
    public void addFilterUrlMapping(FilterMappingConf filterMappingConf) {
        log.warn("#49 addFilterUrlMapping, filterMappingConf=" + filterMappingConf);

        EnumSet<DispatcherType> dispatcherTypes = this.getDispatcherTypeEnumSet(filterMappingConf);

        budoServletServer.getServletContext().getFilterRegistration(filterMappingConf.getFilterName()) //
                .addMappingForUrlPatterns(dispatcherTypes, true, filterMappingConf.getUrlPatternList().toArray(new String[0]));
    }

    @Override
    public void addServlet(ServletConf servletConf) {
        log.warn("#57 addServlet, servletConf=" + servletConf);

        Dynamic dynamic = budoServletServer.getServletContext() //
                .addServlet(servletConf.getServletName(), servletConf.getServletClass().getName());

        dynamic.addMapping(servletConf.getUrlPattern());
        dynamic.setInitParameters(servletConf.getInitParamMap());

        Integer loadOnStartup = servletConf.getLoadOnStartup();
        if (null != loadOnStartup) {
            dynamic.setLoadOnStartup(loadOnStartup);
        }
    }

    @Override
    public void addWelcomeFile(String welcomePage) {
        log.warn("#71 addWelcomeFile, welcomePage=" + welcomePage);

        budoServletServer.getWelcomeFiles().add(welcomePage);
    }

    @Override
    public void addErrorPage(String location, Integer errorCode) {
        log.warn("#75 addErrorPage, location=" + location + ", errorCode=" + errorCode);

        budoServletServer.getErrorPages().put(errorCode, location);
    }
}